package com.cfpl.admin_service.VO;

import com.cfpl.project_common.pojo.Admin;
import com.cfpl.project_common.pojo.Permission;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Transient;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: LYP
 * @Date: 2023/10/13 - 21:17
 * @Description: com.cfpl.admin_service.VO
 * @version: 1.0
 */
@Data
@NoArgsConstructor
public class LoginAdmin implements UserDetails, Serializable {
    //封装成自己的管理员对象
    private Admin admin;
    //存储权限信息
    private List<String> permissionList;

    public LoginAdmin(Admin admin, List<String> permissionList) {
        this.admin = admin;
        this.permissionList = permissionList;
    }

    //Security框架中的权限集合
    @Transient
    private List<GrantedAuthority> authorities;
    //返回权限集合
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities!=null){
            return authorities;
        }

        authorities = permissionList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());

//        for (String permission : permissionList) {
//            authorities.add(new SimpleGrantedAuthority(permission));
//        }
        return authorities;
    }

    //获取密码
    @Override
    public String getPassword() {
        return this.admin.getAdminPassword();
    }
    //获取用户名
    @Override
    public String getUsername() {
        return this.admin.getAdminName();
    }
    //没有过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //账户没有锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //密码没有过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
